from flask import Blueprint, render_template, flash, redirect, url_for, request, jsonify, session
from flask_login import current_user, login_required

from apps.forms.video_library_form import CreateVideoLibraryForm, UpdateVideoLibraryForm, QueryVideoLibraryForm
from rules.video_library_rule import *

video_library = Blueprint('video_library', __name__, url_prefix='/video_library')
ACTIVE = 'library'
ACTIVE_ITEM = 'video_library'
SEARCH_PAGE = 'video_library_page'


@video_library.route('/page', methods=['POST', 'GET'])
@login_required
def page():
    form = QueryVideoLibraryForm(csrf_enabled=False)
    last_search = session.get('last_search')
    page_index = 1
    title = ''
    dy_open_video_id = ''
    dy_sync_status = ''

    if request.method == 'GET':
        if last_search and last_search.get('search_page', '') == SEARCH_PAGE:
            page_index = last_search.get('page_index', 1)
            if last_search.get('search_data'):
                title = last_search.get('search_data').get('title', '')
                dy_open_video_id = last_search.get('search_data').get('dy_open_video_id', '')
                dy_sync_status = last_search.get('search_data').get('dy_sync_status', '')

        page_index = request.args.get('page', page_index, type=int)

        title = request.args.get('title', title)
        dy_open_video_id = request.args.get('dy_open_video_id', dy_open_video_id)
        dy_sync_status = request.args.get('dy_sync_status', dy_sync_status)

        form.title.data = title
        form.dy_open_video_id.data = dy_open_video_id
        form.dy_sync_status.data = dy_sync_status

    else:
        page_index = 1
        if form.title.data:
            title = form.title.data

        if form.dy_open_video_id.data:
            dy_open_video_id = form.dy_open_video_id.data

        if form.dy_sync_status.data:
            dy_sync_status = form.dy_sync_status.data

    search_data = dict(title=title, dy_open_video_id=dy_open_video_id, dy_sync_status=dy_sync_status)
    last_search = dict(search_page=SEARCH_PAGE, page_index=page_index, search_data=search_data)
    session['last_search'] = last_search

    print(last_search)

    pagination = search_video_library_with_page(page_index, search_data)

    return render_template('video_library/page.html', pagination=pagination, form=form, active=ACTIVE,
                           active_item=ACTIVE_ITEM)


@video_library.route('/create', methods=['POST', 'GET'])
@login_required
def create():
    form = CreateVideoLibraryForm()
    if form.validate_on_submit():
        create_video_library(form, current_user)
        flash('新增成功', 'page_success')
        return redirect(url_for('video_library.page'))
    return render_template('video_library/create.html', form=form, active=ACTIVE,
                           active_item=ACTIVE_ITEM)


@video_library.route('/edit/<int:id>', methods=['POST', 'GET'])
@login_required
def edit(id):
    video_library_orm = search_video_library(id)
    if request.method == 'GET':
        form = UpdateVideoLibraryForm(video_library_orm)
    else:
        form = UpdateVideoLibraryForm()

    if form.validate_on_submit():
        try:
            describe_video_library(id, form, current_user)
            flash('修改成功', 'page_success')
            return redirect(url_for('video_library.page'))
        except Exception as e:
            flash(str(e), 'page_error')

    return render_template('video_library/edit.html', form=form, id=id, active=ACTIVE,
                           active_item=ACTIVE_ITEM)


@video_library.route('/list/<keyword>', methods=['GET'])
@login_required
def search_list(keyword):
    try:
        data_param = dict(title=keyword)
        video_list = search_video_library_with_list(data_param)
    except Exception as e:
        video_list = []

    return jsonify({"status": 'success', "msg": '', "data": video_list})
